package com.jozen.modelbase.netwoek.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;


import com.jozen.modelbase.netwoek.Common;
import com.jozen.modelbase.netwoek.base.ModuleBaseApplication;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AppUtil {


    /**
     * 判断是不是卡号
     *
     * @param cardNo
     */
    public static boolean isNumberLong(String cardNo) {
        if (TextUtils.isEmpty(cardNo)) return false;
        try {
            Long.parseLong(cardNo);
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    public static void getScreenWidth(Activity activity) {
        if (Common.screenWidth <= 0) {
            DisplayMetrics dm = new DisplayMetrics();
            // 获取屏幕信息
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            Common.screenWidth = dm.widthPixels;
        }
    }

    public static void getScreenHeight(Activity activity) {
        if (0 >= Common.screenHeight) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            Common.screenHeight = dm.heightPixels;

        }
    }

    /**
     * 根据传入控件的坐标和用户的焦点坐标，判断是否隐藏键盘，如果点击的位置在控件内，则不隐藏键盘
     *
     * @param view  控件view
     * @param event 焦点位置
     * @return 是否隐藏
     */
    public static void hideKeyboard(MotionEvent event, View view, Activity activity) {
        try {
            if (view != null && view instanceof EditText) {
                int[] location = {0, 0};
                view.getLocationInWindow(location);
                int left = location[0], top = location[1], right = left
                        + view.getWidth(), bootom = top + view.getHeight();
                // 判断焦点位置坐标是否在空间内，如果位置在控件外，则隐藏键盘
                if (event.getRawX() < left || event.getRawX() > right
                        || event.getY() < top || event.getRawY() > bootom) {
                    // 隐藏键盘

                    IBinder token = view.getWindowToken();
                    InputMethodManager inputMethodManager = (InputMethodManager) activity
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(token,
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //判断手机号码格式
    public static boolean isPhoneMobile(String number) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(number);
        boolean isMatch = m.matches();
        return isMatch;
    }

    //获取当前程序的versionCode
    public static int getVersionCode() {
        int versioncode = 1;
        PackageManager pm = ModuleBaseApplication.getInstance().getApplicationContext().getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo(ModuleBaseApplication.getInstance().getApplicationContext().getPackageName(), 0);
            versioncode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versioncode;
    }

    //获取当前程序的versionName
    public static String getVersionName() {
        String versionName = "V1.0.0";
        PackageManager pm = ModuleBaseApplication.getInstance().getApplicationContext().getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo(ModuleBaseApplication.getInstance().getApplicationContext().getPackageName(), 0);
            versionName = "V" + packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static String getTime(long time) {
        String currentTime = new SimpleDateFormat("MM-dd HH:mm").format(time);
        return currentTime;
    }


    /**
     * 获取内网ip
     *
     * @return
     */
    public static String getLocalIPAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "null";
    }


    /**
     * 是否是1020X800的小屏一体机
     *
     * @return
     */
    public static boolean isSmallScreen() {
        if (DensityUtil.getHeight(ModuleBaseApplication.getInstance().getApplicationContext()) == 800) {
            return true;
        }
        return false;
    }
}
